<template>
  <view class='container'>
    <view class="left">
      <radio :checked="checkAll" color="#e00000" @click="ALLCHECKED(!checkAll)"></radio>
      全选
    </view>
    <view class="middle">
      合计:￥{{priceSum2}}
    </view>
    <view class="right">
      <button class="btn" @click='login'>结算({{checkedSum}})</button>
    </view>
  </view>
</template>

<script>
  import {mapGetters,mapMutations,mapState} from 'vuex';
  import {CreatePay,prePay,payRes} from '@/api/api.js'
  export default {
    name: "my-settle",
    computed:{
      ...mapState('user',['token']),
      ...mapState('cart',['cart']),
      ...mapGetters('cart',['checkedSum','priceSum','checkAll']),
      ...mapGetters('user',['addressAll']),
      priceSum2(){
        return this.priceSum.toFixed(2);
      }
    },
    methods:{
      ...mapMutations('cart',['ALLCHECKED']),
      ...mapMutations('user',['UPDATEREDIRECINFO']),
      login(){
        if(!this.checkedSum) return uni.$showMsg('请选择要结算的商品');
        if(Object.is(this.addressAll,NaN)) return uni.$showMsg('请选择收获地址');
        if(this.token=='') return this.delayNavigation();
        this.payOrder();
      },
      async payOrder(){
        const orderInfo={
          // order_price:this.priceSum,
          order_price:0.01,
          consignee_addr:this.addressAll,
          goods:this.cart.filter(x=>x.goods_state).map(x=>({goods_id:x.goods_id,goods_number:x.goods_count,goods_price:x.goods_price}))
        };
        const {data:res}=await CreatePay(orderInfo);
        if(res.meta.status!==200){
          uni.$showMsg('缺失token,订单创建失败');
          console.log('缺失token,订单创建失败');
          // const orderNumber=res.message.order_number;
          const orderNumber='12345fweg345';
          const {data:res1}=await prePay({
            order_number:orderNumber
          });
          if(res1.meta.status!==200){
              uni.$showMsg('缺失token,订单预支付失败');
              console.log('缺失token,订单预支付失败');
              // const payInfo=res1.message.pay
              const payInfo={};
              const [err,succ]=await uni.requestPayment(payInfo).catch(err=>err);
              if(err) {
                uni.$showMsg('requestPayment订单未支付！');
                console.log('requestPayment订单未支付！');
                const {data:res3}=await payRes({
                  order_number:orderNumber
                });
                if(res1.meta.status!==200){
                  uni.$showMsg('订单未支付！');
                  console.log('订单未支付！');
                  uni.showToast({
                    title:'支付完成！',
                    icon:'success'
                  })
                }else{
                  uni.showToast({
                    title:'支付完成！',
                    icon:'success'
                  })
                }
              }
          }
        }
      },
      showTips(n){
        uni.showToast({
          icon:'none',
          title:'请登录后再结算！'+n+'秒后跳转到登录页',
          mask:true,
          duration:1500
        })
      },
      delayNavigation(){
        this.showTips(this.second);
        let timer=setInterval(()=>{
          this.second--;
          this.showTips(this.second)
          if(this.second==0){
            clearInterval(timer);
            this.second=3;
            uni.switchTab({
              url:'../../pages/my/my',
              success: () => {
                this.UPDATEREDIRECINFO({
                  'openType':'switchTab',
                  'from':'/pages/cart/cart'
                })
              }
            })
          }
        },1000)
      }
    },
    data() {
      return {
        second:3
      };
    }
  }
</script>

<style lang="scss" scoped>
  .container {
    width: 750rpx;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 50px;
    background-color: #fff;
    padding: 0 10px;
    box-sizing: border-box;
    .right {
      .btn {
        background-color: #e00000;
        width: 100px;
        font-size: 14px;
        height: 100%;
      }
    }
  }
</style>
